/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Settings;
using MyPhotoIndex.DataAccess;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.ImageBank.Query;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Plugins.DataProviders;
using System.Windows.Forms;

namespace MyPhotoIndex
{
    partial class Main
    {
        public static void CreateInitialPhotoIndex()
        {
            string dbPath = Path.Combine(ApplicationFolders.DefaultIndexFolder, ApplicationFolders.DefaultIndexFileName);
         
            if (Directory.Exists(ApplicationFolders.DefaultIndexFolder) == false)
            {
                Directory.CreateDirectory(ApplicationFolders.DefaultIndexFolder);
            }

            CreateNewPhotoIndex(dbPath);

            ApplicationSettings.Default.IndexDb = dbPath;
            ApplicationSettings.Default.Save();
        }

        public static void CreateNewPhotoIndex(string path)
        {
            try
            {
                String dataFile = Path.Combine(ApplicationFolders.ApplicationFolder, "Data");
                File.Copy(Path.Combine(dataFile, ApplicationFolders.DefaultIndexFileName), path);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }

        private bool ConnectToPhotoIndex(string path)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                bool connect = DataAccessProvider.Instance.Connect(path);
                if (connect == false)
                {
                    this.Cursor = Cursors.Default;
                    return false;
                }

                UpdateRecentIndexList(path);
                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                Logger.Log(ex);
                return false;
            }

            return true;
        }

        private void UpdateRecentIndexList(string path)
        {
            string indexId = DataAccessProvider.Instance.GetDbIndexId();
            string indexName = DataAccessProvider.Instance.GetDbIndexName();
            int indexImageCount = DataAccessProvider.Instance.GetImageListSize();

            ApplicationSettings.Default.RecentlyUsedIndexes.UpdateIndexInfo(indexId, path, indexName, indexImageCount);

            this.Text = "My Photo Index - " + path;
        }

        private bool SelectPhotoIndex()
        {
            SelectPhotoIndexForm sfif = new SelectPhotoIndexForm();
            DialogResult dr = sfif.ShowDialog(this);

            if (dr != System.Windows.Forms.DialogResult.OK ||
                string.IsNullOrEmpty(sfif.FileSelected) == true)
            {
                return false;
            }

            String fileSelected = sfif.FileSelected;

            if (File.Exists(fileSelected) == false &&
                sfif.CreateNew == true)
            {
                CreateNewPhotoIndex(fileSelected);
            }

            this.Cursor = Cursors.WaitCursor;
            bool connect = DataAccessProvider.Instance.Connect(fileSelected);
            if (connect == false)
            {
                this.Cursor = Cursors.Default;
                return false;
            }

            ApplicationSettings.Default.IndexDb = fileSelected;
            DynamicConfiguration.Default.Save();
            UpdateRecentIndexList(fileSelected);
            this.Cursor = Cursors.Default;

            return true;
        }

        private bool SwitchIndex()
        {
            if (SelectPhotoIndex() == false)
            {
                return false;
            }

            PhotoIndexUpgrader upgrader = new PhotoIndexUpgrader();
            bool indexNeedsUpgrade = upgrader.NeedUpgrade();
            bool cacheNeedsUpgrade = upgrader.ImageCacheNeedUpgrade();

            if (indexNeedsUpgrade == true ||
                cacheNeedsUpgrade == true)
            {
                try
                {
                    if (indexNeedsUpgrade == true)
                    {
                        using (m_progressForm = new AsyncProgressForm(this, Resources.Resources.UpgradingPhotoIndex, null, DataAccessProvider.Instance.GetImageListSize()))
                        {
                            upgrader.UpgradePhotoIndexData(m_progressForm);
                        }
                    }

                    if (cacheNeedsUpgrade == true)
                    {
                        using (m_progressForm = new AsyncProgressForm(this, Resources.Resources.UpgradeImageCache, null, 0))
                        {
                            upgrader.UpgradeImageCache(m_progressForm);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }

            ClearSelectedPane();
            ClearTagsPalette();
            photoThumbViewer1.ClearSelection(true);
            ImageCategoryCollection.Instance.Load((IImageCategoriesDataAccessProvider)DataAccessProvider.Instance);
            BuildTagTree();
            Storage.Instance.LoadImageList();
            Storage.Instance.Filter((QueryParameter)null);
            BuildFolderTree();
            RefreshTagsPalette();

            return true;
        }
    }
}
